package com.zzhua.servlet;

import javax.servlet.*;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 自servlet3.0开始, tomcat支持使用注解的方式代替web.xml配置的方式, 向web容器中注册Servlet,Filter,Listener
 */
@WebServlet(
        urlPatterns = {"/anno"} , // 不能丢“/”
        initParams = {            // 配置初始化参数
                @WebInitParam(name = "name",value="zzhua"),
                @WebInitParam(name = "age",value="25")
        }
)
public class AnnoServlet implements Servlet {

    private static ServletConfig SERVLET_CONFIG;

    private static ServletContext SERVLET_CONTEXT = null;

    public AnnoServlet() {
        System.out.println(1);
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String initParamName = initParameterNames.nextElement();
            System.out.println(initParamName + " - " + servletConfig.getInitParameter(initParamName));
        }
        SERVLET_CONTEXT = servletConfig.getServletContext();
        AnnoServlet.SERVLET_CONFIG = servletConfig;
    }

    @Override
    public ServletConfig getServletConfig() {
        return AnnoServlet.SERVLET_CONFIG;
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        System.out.println("我拿到了req和res");
    }

    @Override
    public String getServletInfo() {
        return "annoServlet";
    }

    @Override
    public void destroy() {

    }
}
